<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>收货地址</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link type="text/css" rel="stylesheet" href="statics/css/style.css" />

<script type="text/javascript" src="/EasyBuy/statics/js/area.js"></script>
<script type="text/javascript" src="/EasyBuy/statics/js/menu.js"></script>
<script type="text/javascript" src="/EasyBuy/statics/js/select.js"></script>
<script type="text/javascript" src="/EasyBuy/statics/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="/EasyBuy/statics/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		_init_area();//调用插件
	});
	
	$(document).ready(function(){
		$(".addre").attr("class","now");
		var name=$("#name").val();
		var email=$("#email").val();
		var address=$("#address").val();
		var phone=$("#phone").val();
		
		
		$("#name").blur(function(){
			if($("#name").val()==""||$("#name").val()==null){
				$("#namespan").css("color","red");
				$("#namespan").text("收货人姓名不能为空");
			}else if($("#name").val()!=""||$("#name").val()!=null){
				$("#namespan").css("color","black");
				$("#namespan").text("");
			}
		});
		
		$("#address").blur(function(){
			if($("#address").val()==""||$("#address").val()==null){
				$("#addressspan").css("color","red");
				$("#addressspan").text("地址不能为空");
			}else if($("#address").val()!=""||$("#address").val()!=null){
				$("#addressspan").css("color","black");
				$("#addressspan").text("");
			}
		});
		
		//手机号正则
		var phonepattern= /^0{0,1}(13[0-9]|15[7-9]|153|156|18[4-9])[0-9]{8}$/;	//手机号正则
		
		$("#phone").blur(function(){
			if($("#phone").val()==""||$("#phone").val()==null){
				$("#phonespan").css("color","red");
				$("#phonespan").text("手机号不能为空");
			}else if(!phonepattern.test($("#phone").val())){
				$("#phonespan").css("color","red");
				$("#phonespan").text("手机号格式不正确");
			}else if($("#phone").val()!=""||$("#phone").val()!=null){
				$("#phonespan").css("color","black");
				$("#phonespan").text("");
			}
		});
		//邮箱正则
		var emailpattern= /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;	//邮箱正则
		
		
		$("#email").blur(function(){
			if($("#email").val()==""||$("#email").val()==null){
				$("#emailspan").css("color","red");
				$("#emailspan").text("邮箱不能为空");
			}else if(emailpattern.test($("#email").val())==false){
				$("#emailspan").css("color","red");
				$("#emailspan").text("邮箱格式不正确");
			}else if($("#email").val()!=""||$("#email").val()!=null) {
				$("#emailspan").css("color","black");
				$("#emailspan").text("");
			}
		});
		
		 	
		//点击添加按钮
		 $("#insert").click(function (){
		 	$("#name").val("");
			$("#email").val("");
			$("#phone").val("");
			$("#remark").val("");
			$("#address").val("");
			$(".add_b").text("确认添加");   //点击添加按钮   确认修改按钮变为添加按钮
			
			$(".add_tab tr:last").find("td").eq(2).text("备注");
		});
			$(".add_b").click(function (){
				if($("#email").val()==""||$("#email").val()==null){
					$("#emailspan").css("color","red");
					$("#emailspan").text("邮箱不能为空");
				}else if($("#phone").val()==""||$("#phone").val()==null){
					$("#phonespan").css("color","red");
					$("#phonespan").text("手机号不能为空");
				}else if($("#address").val()==""||$("#address").val()==null){
					$("#addressspan").css("color","red");
					$("#addressspan").text("地址不能为空");
				}else if($("#name").val()==""||$("#name").val()==null){
					$("#namespan").css("color","red");
					$("#namespan").text("收货人姓名不能为空");
				}else{
					var	region=$("#s_province").val()+$("#s_city").val()+$("#s_county").val();
					if($(".add_b").text()=="确认添加"){
						$.get("/EasyBuy/AddressServlet","opr=insert&userid=${applicationScope.id}&name="+$("#name").val()+"&email="+$("#email").val()+"&phone="+$("#phone").val()+"&remark="+$("#remark").val()+"&address="+$("#address").val()+"&region="+region,function(data){
							if(data == "cg"){
								alert("添加成功");
								location.href = "/EasyBuy/backend/order/Member_Address.jsp";
							}
						});
					}
				}
				
			});
			
			var g = "";
			$(".add_b").click(function (){
				if($("#email").val()==""||$("#email").val()==null){
					$("#emailspan").css("color","red");
					$("#emailspan").text("邮箱不能为空");
				}else if($("#phone").val()==""||$("#phone").val()==null){
					$("#phonespan").css("color","red");
					$("#phonespan").text("手机号不能为空");
				}else if($("#address").val()==""||$("#address").val()==null){
					$("#addressspan").css("color","red");
					$("#addressspan").text("地址不能为空");
				}else{
					var	region=$("#s_province").val()+$("#s_city").val()+$("#s_county").val();
					if($(".add_b").text()=="确认修改"){
						$.get("/EasyBuy/AddressServlet","opr=update&id="+g+"&name="+$("#name").val()+"&email="+$("#email").val()+"&phone="+$("#phone").val()+"&remark="+$("#remark").val()+"&address="+$("#address").val()+"&region="+region,function(data){
							if(data == "cg"){
								alert("修改成功");
								location.href = "/EasyBuy/backend/order/Member_Address.jsp";
							}
						});
					}
				}
			});
		
		//查询登录用户的所有收获地址
		function select(){
			$.getJSON("AddressServlet","opr=select&userid=${applicationScope.id}",selectCallBack);
		}
		function selectCallBack(data){
			
			var $table=$("#table").html("");
			$table.append("<tr><td>收货人</td><td>配送区域</td><td>详细地址</td><td>标志建筑</td><td>电话/手机号</td><td>电子邮箱</td><td>操作</td><td></td></tr>");
			for(var i=0;i<data.length;i++){
				if(data[i].isDefault==1){
					$table.append("<tr id='c_"+data[i].id+"'><td class='xname'>"+data[i].name+"</td><td class='xregion'>"+data[i].region+"</td><td class='xaddress'>"+data[i].address+"</td><td class='xremark'>"+data[i].remark+"</td><td class='xphone'>"+data[i].telephone+"</td><td class='xemail'>"+data[i].email+"</td><td><a class='bj' id='"+data[i].id+"' >&nbsp;修改&nbsp;</a>|<a class='del'>&nbsp;删除&nbsp;</a></td><td>默认地址</td></tr>");
				}else if(data[i].isDefault==0){
					$table.append("<tr id='c_"+data[i].id+"'><td class='xname'>"+data[i].name+"</td><td class='xregion'>"+data[i].region+"</td><td class='xaddress'>"+data[i].address+"</td><td class='xremark'>"+data[i].remark+"</td><td class='xphone'>"+data[i].telephone+"</td><td class='xemail'>"+data[i].email+"</td><td><a class='bj' id='"+data[i].id+"'>&nbsp;修改&nbsp;</a>|<a class='del'>&nbsp;删除&nbsp;</a></td><td><a class='update' id='"+data[i].id+"'>设为默认地址</a></td></tr>");
				}
			}
			
			$(".update").click(function (){   //设为默认地址时 用userid(2) 和 标志地址来设置
				$.get("/EasyBuy/AddressServlet","opr=updatedefault&userId=${applicationScope.id}&id="+this.id,function(data){
					if(data == ("cg")){
						alert("设置成功");
					}else{
						alert("设置失败,请重新设置");
					}
					location.href="/EasyBuy/backend/order/Member_Address.jsp";
				});
			});
			
			//点击编辑,信息填写到文本框,修改信息
			$(".bj").click(function (){
				g = this.id;
				$.getJSON("/EasyBuy/AddressServlet","opr=getAllinfo&id="+this.id,call);
					$(".add_tab tr:last").find("td").eq(2).text("标志建筑");
					function call(data){
						for(var i=0;i<data.length;i++){
						$("#name").val(data[i].name);
						$("#email").val(data[i].email);
						$("#phone").val(data[i].telephone);
						$("#remark").val(data[i].remark);
						$("#address").val(data[i].address);
						$(".add_b").text("确认修改");   //点击添加按钮   确认修改按钮变为添加按钮
						$(".add_b").attr("title",data[i].id);
					}
				}
		});	
			//删除地址
			$(".del").click(function(){
				var del=confirm("确定要删除吗?");
				var s = $(this).parent().parent().get(0).id;
				s = s.substring(s.lastIndexOf("c_")+2);	//分割￥符号
				if(del == true){
					$.get("/EasyBuy/AddressServlet","opr=delete&id="+s,function(data){
						if(data == "cg"){
							alert("删除成功!");
						}else if(data == "sb"){
							alert("删除失败,请联系管理员!");
						}
					});
					location.href = "/EasyBuy/backend/order/Member_Address.jsp";
				}
				
			});
		}
		select();
		
	});
</script>

<title>尤洪</title>
</head>
<body>

  	
  	<c:choose>
	  	<c:when test="${empty applicationScope.id}">
	  		<% response.sendRedirect("/EasyBuy/pre/Login.jsp");%>
	  	</c:when>
	  	<c:otherwise>
	  		<%@ include file="../../common/header.jsp" %>
			<%@ include file="../../common/backendHeader.jsp" %>
			 <c:choose>
				<c:when test="${empty applicationScope.type }">
					<% 	request.getRequestDispatcher("../../UserServlet?opr=type&href=Member_Address&href1=order").forward(request, response); %>
				</c:when>
				<c:otherwise>
					<c:if test="${applicationScope.type==1 }">
		  				<%@ include file="../../common/sideMamber.jsp" %> 
					</c:if>
					<c:if test="${applicationScope.type==0 }">
						<%@ include file="../../common/sideUser.jsp" %> 
					</c:if>
				</c:otherwise>
			 </c:choose>
	  	</c:otherwise>
  	</c:choose>
	
	
			<div class="m_right">
				<p></p>
				<div class="mem_tit">收货地址</div>
				<div>
					<table id="table" border="1" class="order_tab" align="center"
						style="width:98%; margin:10px auto;" cellspacing="0"
						cellpadding="0">
					</table>
				</div>

				<div class="mem_tit">   <!-- 添加图片  -->
					<a id="insert"><img src="/EasyBuy/statics/images/add_ad.gif" />
					</a>
				</div>
				<table border="0" class="add_tab" style="width:930px;"
					cellspacing="0" cellpadding="0">
					<tr>
						<td width="135" align="right">配送地区</td>
						<td><select name="s_province" id="s_province"></select>
						<select name="s_city" id="s_city"></select>
						<select name="s_county" id="s_county"></select></td>
					</tr>
					<tr>
						<td align="right">收货人姓名</td>
						<td style="font-family:'宋体';"><input type="text" placeholder="张三"
							class="add_ipt" id="name"/><font size=1>&nbsp;<span id="namespan">（必填）</span></font></td>
						<td align="right">电子邮箱</td>
						<td style="font-family:'宋体';"><input type="text" id="email"
							placeholder="12345678@qq.com" class="add_ipt" /><font size=1>&nbsp;<span id="emailspan">（必填）</span></font></td>
					</tr>
					
					<tr>
						<td align="right">详细地址</td>
						<td style="font-family:'宋体';"><input type="text" placeholder="世外桃源" id="address"
							class="add_ipt" /><font size=1>&nbsp;<span id="addressspan">（必填）</span></font></td>
						<td align="right">邮政编码</td>
						<td style="font-family:'宋体';"><input type="text" 
							placeholder="610000" class="add_ipt" />
						</td>
					</tr>
					<tr>
						<td align="right">手机</td>
						<td style="font-family:'宋体';"><input type="text" id="phone"
							placeholder="1361234587" class="add_ipt" /><font size=1>&nbsp;<span id="phonespan">（必填）</span></font></td>
						<td align="right">标志建筑</td>
						<td style="font-family:'宋体';"><input type="text"
							placeholder="世外桃源大酒店" class="add_ipt" id="remark"/>
						</td>
					</tr>
				</table>
				<p align="right">
					<a class="add_b">确认添加</a>
				</p> 
			</div>
		<%@ include file="../../common/footer.jsp" %>

</body>
</html>